{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a83ab33d",
   "metadata": {},
   "source": [
    "# 元组\n",
    "元组用于在单个变量中存储多个项目。\n",
    "\n",
    "Tuple 是 Python 中用于存储数据集合的 4 种内置数据类型之一，另外 3 种是 List、Set 和 Dictionary，它们都有不同的性质和用法。\n",
    "\n",
    "元组是有序且不可更改的集合。\n",
    "\n",
    "元组是用圆括号写的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3586bcf9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('apple', 'banana', 'cherry')\n"
     ]
    }
   ],
   "source": [
    "thistuple = (\"apple\", \"banana\", \"cherry\")\n",
    "print(thistuple)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bba0e9b2",
   "metadata": {},
   "source": [
    "## 元组项目\n",
    "元组项是有序的、不可更改的，并允许重复值。\n",
    "\n",
    "元组项目被索引，第一个项目有索引 [0]，第二个项目有索引 [1] 等等。\n",
    "\n",
    "### 有序的\n",
    "当我们说元组是有序的时，这意味着项目具有定义的顺序，并且该顺序不会改变。\n",
    "\n",
    "### 不可改变的\n",
    "元组是不可更改的，这意味着我们不能在元组创建后更改、添加或删除项目。\n",
    "\n",
    "### 允许重复\n",
    "由于元组被索引，它们可以具有相同值的项目："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7e3a0959",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('apple', 'banana', 'cherry', 'apple', 'cherry')\n"
     ]
    }
   ],
   "source": [
    "thistuple = (\"apple\", \"banana\", \"cherry\", \"apple\", \"cherry\")\n",
    "print(thistuple)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "426e5da4",
   "metadata": {},
   "source": [
    "## 元组长度\n",
    "要确定元组有多少项，请使用 len() 函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d641ea39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "thistuple = (\"apple\", \"banana\", \"cherry\")\n",
    "print(len(thistuple))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce879eb0",
   "metadata": {},
   "source": [
    "## 使用一项创建元组\n",
    "要创建只有一项的元组，必须在该项后添加一个逗号，否则 Python 不会将其识别为元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5249185f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "thistuple = (\"apple\",)\n",
    "print(type(thistuple))\n",
    "\n",
    "#NOT a tuple\n",
    "thistuple = (\"apple\")\n",
    "print(type(thistuple))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a646bec",
   "metadata": {},
   "source": [
    "## 元组项 - 数据类型\n",
    "元组项可以是任何数据类型："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8ff0353c",
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple1 = (\"apple\", \"banana\", \"cherry\")\n",
    "tuple2 = (1, 5, 7, 9, 3)\n",
    "tuple3 = (True, False, False)\n",
    "\n",
    "tuple4 = (\"abc\", 34, True, 40, \"male\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebecea9a",
   "metadata": {},
   "source": [
    "## tuple() 构造函数\n",
    "也可以使用 tuple() 构造函数来创建元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "074dab9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('apple', 'banana', 'cherry')\n"
     ]
    }
   ],
   "source": [
    "thistuple = tuple((\"apple\", \"banana\", \"cherry\")) # 注意双括号\n",
    "print(thistuple)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d81d7ee",
   "metadata": {},
   "source": [
    "# 访问元组项目\n",
    "通过引用方括号内的索引号来访问元组项："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0710af34",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "banana\n"
     ]
    }
   ],
   "source": [
    "thistuple = (\"apple\", \"banana\", \"cherry\")\n",
    "print(thistuple[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "492cfda9",
   "metadata": {},
   "source": [
    "## 负索引\n",
    "负索引意味着从尾开始。\n",
    "\n",
    "-1 指最后一项，-2 指倒数第二项，以此类推。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fb2d04d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cherry\n"
     ]
    }
   ],
   "source": [
    "thistuple = (\"apple\", \"banana\", \"cherry\")\n",
    "print(thistuple[-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b6076b5",
   "metadata": {},
   "source": [
    "## 索引范围\n",
    "您可以通过指定范围的开始和结束位置来指定索引范围。\n",
    "\n",
    "指定范围时，返回值将是具有指定项的新元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e14bd1ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('cherry', 'orange', 'kiwi')\n"
     ]
    }
   ],
   "source": [
    "thistuple = (\"apple\", \"banana\", \"cherry\", \"orange\", \"kiwi\", \"melon\", \"mango\")\n",
    "print(thistuple[2:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2db83293",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('apple', 'banana', 'cherry', 'orange')\n"
     ]
    }
   ],
   "source": [
    "# 通过省略起始值，范围将从第一项开始：\n",
    "thistuple = (\"apple\", \"banana\", \"cherry\", \"orange\", \"kiwi\", \"melon\", \"mango\")\n",
    "print(thistuple[:4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f63ad816",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('cherry', 'orange', 'kiwi', 'melon', 'mango')\n"
     ]
    }
   ],
   "source": [
    "# 通过省略结束值，范围将继续到列表的末尾：\n",
    "thistuple = (\"apple\", \"banana\", \"cherry\", \"orange\", \"kiwi\", \"melon\", \"mango\")\n",
    "print(thistuple[2:])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9c5111d",
   "metadata": {},
   "source": [
    "## 负索引范围\n",
    "如果要从元组末尾开始搜索，请指定负索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "174884f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('orange', 'kiwi', 'melon')\n"
     ]
    }
   ],
   "source": [
    "thistuple = (\"apple\", \"banana\", \"cherry\", \"orange\", \"kiwi\", \"melon\", \"mango\")\n",
    "print(thistuple[-4:-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f22adfc3",
   "metadata": {},
   "source": [
    "## 检查项目是否存在\n",
    "要确定元组中是否存在指定的项目，请使用 in 关键字："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "06cc29b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Yes, 'apple' is in the fruits tuple\n"
     ]
    }
   ],
   "source": [
    "thistuple = (\"apple\", \"banana\", \"cherry\")\n",
    "if \"apple\" in thistuple:\n",
    "  print(\"Yes, 'apple' is in the fruits tuple\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb208986",
   "metadata": {},
   "source": [
    "# 更新元组\n",
    "元组是不可更改的，这意味着一旦创建了元组，您就无法更改、添加或删除项目。\n",
    "\n",
    "但是有一些解决方法。\n",
    "\n",
    "## 更改元组值\n",
    "一旦创建了元组，就不能更改其值。 元组是不可更改的，或不可变的。\n",
    "\n",
    "但是有一个解决方法。 将元组转换为列表，更改列表，然后将列表转换回元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7ba0055e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('apple', 'kiwi', 'cherry')\n"
     ]
    }
   ],
   "source": [
    "x = (\"apple\", \"banana\", \"cherry\")\n",
    "y = list(x)\n",
    "y[1] = \"kiwi\"\n",
    "x = tuple(y)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98e5b034",
   "metadata": {},
   "source": [
    "## 添加项目\n",
    "由于元组是不可变的，它们没有内置的 append() 方法，但还有其他方法可以向元组添加项。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9e79e97b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转换为列表：就像更改元组的解决方法一样，将其转换为列表，添加您的项目，然后将其转换回元组。\n",
    "thistuple = (\"apple\", \"banana\", \"cherry\")\n",
    "y = list(thistuple)\n",
    "y.append(\"orange\")\n",
    "thistuple = tuple(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fa8787e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('apple', 'banana', 'cherry', 'orange')\n"
     ]
    }
   ],
   "source": [
    "# 将元组添加到元组。 向元组添加元组，因此如果想添加一个（或多个）项目，请使用该项目创建一个新元组，并将其添加到现有元组中\n",
    "thistuple = (\"apple\", \"banana\", \"cherry\")\n",
    "y = (\"orange\",)\n",
    "thistuple += y\n",
    "\n",
    "print(thistuple)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0cbc485c",
   "metadata": {},
   "source": [
    "## 删除项目\n",
    "元组是不可更改的，因此不能从中删除项目，但可以使用与我们用于更改和添加元组项目相同的解决方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fa4953cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "thistuple = (\"apple\", \"banana\", \"cherry\")\n",
    "y = list(thistuple)\n",
    "y.remove(\"apple\")\n",
    "thistuple = tuple(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "dcb56c07",
   "metadata": {},
   "outputs": [],
   "source": [
    "thistuple = (\"apple\", \"banana\", \"cherry\")\n",
    "del thistuple"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38754d39",
   "metadata": {},
   "source": [
    "# 解包元组\n",
    "当我们创建一个元组时，我们通常会为其分配值。 这称为“打包”元组\n",
    "\n",
    "但是，在 Python 中，我们也可以将值提取回变量中。 这称为“解包”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "62b502db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "banana\n",
      "cherry\n"
     ]
    }
   ],
   "source": [
    "fruits = (\"apple\", \"banana\", \"cherry\")\n",
    "\n",
    "(green, yellow, red) = fruits\n",
    "\n",
    "print(green)\n",
    "print(yellow)\n",
    "print(red)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a56ab145",
   "metadata": {},
   "source": [
    "## 使用星号*\n",
    "如果变量的数量少于值的数量，您可以在变量名后添加 *，这些值将作为列表分配给变量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7ea4ad4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "banana\n",
      "['cherry', 'strawberry', 'raspberry']\n"
     ]
    }
   ],
   "source": [
    "fruits = (\"apple\", \"banana\", \"cherry\", \"strawberry\", \"raspberry\")\n",
    "\n",
    "(green, yellow, *red) = fruits\n",
    "\n",
    "print(green)\n",
    "print(yellow)\n",
    "print(red)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "53479568",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "['mango', 'papaya', 'pineapple']\n",
      "cherry\n"
     ]
    }
   ],
   "source": [
    "# 如果星号被添加到另一个变量名而不是最后一个，Python 将为变量赋值，直到剩余的值数量与剩余的变量数匹配。\n",
    "fruits = (\"apple\", \"mango\", \"papaya\", \"pineapple\", \"cherry\")\n",
    "\n",
    "(green, *tropic, red) = fruits\n",
    "\n",
    "print(green)\n",
    "print(tropic)\n",
    "print(red)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d22cbba2",
   "metadata": {},
   "source": [
    "# 遍历元组\n",
    "可以使用 for 循环遍历元组项。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "9f04630f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "banana\n",
      "cherry\n"
     ]
    }
   ],
   "source": [
    "thistuple = (\"apple\", \"banana\", \"cherry\")\n",
    "for x in thistuple:\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9a3c206",
   "metadata": {},
   "source": [
    "## 遍历索引号\n",
    "还可以通过引用它们的索引号来循环遍历元组项。\n",
    "\n",
    "使用 range() 和 len() 函数创建合适的可迭代对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9a5e57a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "banana\n",
      "cherry\n"
     ]
    }
   ],
   "source": [
    "thistuple = (\"apple\", \"banana\", \"cherry\")\n",
    "for i in range(len(thistuple)):\n",
    "  print(thistuple[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02796d99",
   "metadata": {},
   "source": [
    "## 使用 While 循环\n",
    "可以使用 while 循环遍历列表项。\n",
    "\n",
    "使用 len() 函数来确定元组的长度，然后从 0 开始并通过引用它们的索引来循环遍历元组项。\n",
    "\n",
    "请记住在每次迭代后将索引增加 1。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "433f4bcf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "banana\n",
      "cherry\n"
     ]
    }
   ],
   "source": [
    "thistuple = (\"apple\", \"banana\", \"cherry\")\n",
    "i = 0\n",
    "while i < len(thistuple):\n",
    "  print(thistuple[i])\n",
    "  i = i + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35c8dab6",
   "metadata": {},
   "source": [
    "# 元组连接\n",
    "## 连接两个元组\n",
    "要连接两个或多个元组，可以使用 + 运算符："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "40a93786",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', 'b', 'c', 1, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "tuple1 = (\"a\", \"b\" , \"c\")\n",
    "tuple2 = (1, 2, 3)\n",
    "\n",
    "tuple3 = tuple1 + tuple2\n",
    "print(tuple3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7555e533",
   "metadata": {},
   "source": [
    "## 元组乘法\n",
    "如果要将元组的内容乘以给定次数，可以使用 * 运算符："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "1b51bdac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')\n"
     ]
    }
   ],
   "source": [
    "fruits = (\"apple\", \"banana\", \"cherry\")\n",
    "mytuple = fruits * 2\n",
    "\n",
    "print(mytuple)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
